package com.ae.shield.common.items.shieldFittings.makers;

import com.ae.shield.common.enchantment.shieldSuffix.SuffixList;
import com.ae.shield.common.items.util.ItemNBTHelper;
import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.potion.EffectInstance;
import net.minecraft.potion.Effects;
import net.minecraft.stats.Stats;
import net.minecraft.util.*;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;
import net.minecraftforge.energy.IEnergyStorage;

import javax.annotation.Nonnull;
import java.util.List;

public class UndyingProtection extends ShieldMakerBase {
    private final String TAG_TOTEM = "totem";

    public UndyingProtection() {
        super(new Properties(), new ShieldProperty()
                .recovery(0.075f)
                .cd(16f)
                .maxIntensity(24f)
                .close(1f)
                .remote(1f)
                .magic(1.2f)
                .other(0f)
                .upperLimit(30f)
                .floorLimit(0f)
                .maxPiles(2)
                .penalty(10f)
                .cost(6));
    }
//TODO　防御效果未起作用
    @Override
    public void addCtrlInformation(List<ITextComponent> components, ItemStack stack) {
        components.add(new TranslationTextComponent("tooltip.ae.shield.undying_protection.tip1").mergeStyle(TextFormatting.GRAY));
        components.add(new TranslationTextComponent("tooltip.ae.shield.undying_protection.tip2").mergeStyle(TextFormatting.GRAY));
        components.add(new TranslationTextComponent("tooltip.ae.shield.undying_protection.tip3").mergeStyle(TextFormatting.GRAY));
        components.add(new TranslationTextComponent("tooltip.ae.shield.undying_protection.tip4").mergeStyle(TextFormatting.GRAY));
        components.add(new TranslationTextComponent("tooltip.ae.shield.undying_protection.tip5").mergeStyle(TextFormatting.GRAY));
        components.add(new TranslationTextComponent("tooltip.ae.shield.undying_protection.tip6").mergeStyle(TextFormatting.GRAY));
        components.add(new TranslationTextComponent("tooltip.ae.shield.undying_protection.tip7").mergeStyle(TextFormatting.GRAY));
        components.add(new TranslationTextComponent("tooltip.ae.shield.undying_protection.tip8").mergeStyle(TextFormatting.GRAY));
        components.add(new TranslationTextComponent("tooltip.ae.shield.undying_protection.tip9").mergeStyle(TextFormatting.BLUE).mergeStyle(TextFormatting.ITALIC));
        components.add(new TranslationTextComponent("tooltip.ae.shield.undying_protection.tip10").mergeStyle(TextFormatting.BLUE).mergeStyle(TextFormatting.ITALIC));
    }

    @Override
    public void addShiftInformation(List<ITextComponent> components, ItemStack stack) {
        components.add(new TranslationTextComponent("tooltip.ae.shield.undying_protection.tip11")
                .append(new StringTextComponent(String.valueOf(ItemNBTHelper.getInt(stack, TAG_TOTEM, 0))))
                .mergeStyle(TextFormatting.BLUE));
    }

    @Override
    public boolean onEntityDeath(ItemStack stack, LivingEntity livingEntity, DamageSource source, IEnergyStorage energyStorage) {
        int totem = ItemNBTHelper.getInt(stack, TAG_TOTEM, 0);

        if (totem < 1) {
            return false;
        }

        ItemNBTHelper.setInt(stack, TAG_TOTEM, ItemNBTHelper.getInt(stack, TAG_TOTEM, 0) - 1);

        if (livingEntity instanceof ServerPlayerEntity) {
            ServerPlayerEntity serverPlayerEntity = (ServerPlayerEntity) livingEntity;
            serverPlayerEntity.addStat(Stats.ITEM_USED.get(Items.TOTEM_OF_UNDYING));
            CriteriaTriggers.USED_TOTEM.trigger(serverPlayerEntity, new ItemStack(Items.TOTEM_OF_UNDYING, 1));
        }

        int level = EnchantmentHelper.getEnchantmentLevel(SuffixList.LEVEL_UP.get(), stack);
        livingEntity.setHealth(1f);
        livingEntity.clearActivePotions();
        livingEntity.addPotionEffect(new EffectInstance(Effects.REGENERATION, 900 + level * 100, 1));
        livingEntity.addPotionEffect(new EffectInstance(Effects.ABSORPTION, 100 + level * 100, 1 + level));
        livingEntity.addPotionEffect(new EffectInstance(Effects.FIRE_RESISTANCE, 800 + level * 100, 0));
        livingEntity.world.setEntityState(livingEntity, (byte) 35);

        return true;
    }

    @Nonnull
    @Override
    public ActionResult<ItemStack> onItemRightClick(@Nonnull World worldIn, PlayerEntity playerIn, @Nonnull Hand handIn) {
        if (!worldIn.isRemote) {
            ItemStack stack = playerIn.inventory.offHandInventory.get(0);
            if (!stack.isEmpty() && stack.getItem() == Items.TOTEM_OF_UNDYING) {
                NonNullList<ItemStack> mainInventory = playerIn.inventory.mainInventory;
                stack.shrink(1);
                int count = 1;
                for (ItemStack itemStack : mainInventory) {
                    if (itemStack.getItem() == Items.TOTEM_OF_UNDYING) {
                        count = count + 1;
                        itemStack.shrink(1);
                    }
                }

                ItemNBTHelper.setInt(playerIn.getHeldItemMainhand(), TAG_TOTEM, ItemNBTHelper.getInt(playerIn.getHeldItemMainhand(), TAG_TOTEM, 0) + count);
                worldIn.playSound(null, playerIn.chasingPosX, playerIn.chasingPosY, playerIn.chasingPosZ, SoundEvents.ENTITY_PLAYER_LEVELUP, SoundCategory.PLAYERS, 5f, 1f);
            }
        }
        return super.onItemRightClick(worldIn, playerIn, handIn);
    }
}
